package com.sskj.mine;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.text.TextUtils;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppDirectCheckUtil;
import com.sskj.lib.util.AppInnerCheckUtil;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.verify.VerifyFirstActivity;
import com.sskj.mine.verify.VerifySecondActivity;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_SECURITY_CENTER)
public class SecurityActivity extends BaseActivity<SecurityPresenter> {

    @BindView(R2.id.menu_verify)
    SuperTextView menuVerify;
    @BindView(R2.id.menu_verify_high)
    SuperTextView menuVerifyHigh;
    @BindView(R2.id.menu_bind_mobile)
    SuperTextView menuBindMobile;
    @BindView(R2.id.menu_login_password)
    SuperTextView menuLoginPassword;
    @BindView(R2.id.menu_pay_password)
    SuperTextView menuPayPassword;
    @BindView(R2.id.security_img)
    ImageView securityImg;
    private UserData mUserInfo;
    @Inject
    UserViewModel userViewModel;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_security;
    }

    @Override
    public SecurityPresenter getPresenter() {
        return new SecurityPresenter();
    }

    @Override
    public void initView() {
        setTitle(getString(R.string.mine_securityActivity1));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                mUserInfo = users.get(0);
            } else {
                mUserInfo = null;
            }
        });
    }

    @Override
    public void initData() {
        //初级认证
        ClickUtil.click(menuVerify, () -> {
            if (mUserInfo == null) {
                return;
            }
            switch (mUserInfo.getStatus()) {  // 实名认证状态
                case 1:
                    VerifyFirstActivity.start(this);
                    break;
                case 2:
                    ToastUtil.showShort(getString(R.string.mine_securityActivity2));
                    break;
                case 3:
                    ToastUtil.showShort(getString(R.string.mine_securityActivity3));
                    break;
                case 4:
                    ToastUtil.showShort(getString(R.string.mine_securityActivity4));
                    break;
            }
        });
        //高级认证
        ClickUtil.click(menuVerifyHigh, () -> {
            if (!AppInnerCheckUtil.checkTradePwd(this, mUserInfo, getString(R.string.mine_securityActivity5))) {
                return;
            }
            switch (mUserInfo.getAuth_status()) {  // 高级实名认证
                case 1:
                    VerifySecondActivity.start(this);
                    break;
                case 2:
                    ToastUtil.showShort(getString(R.string.mine_securityActivity6));
                    break;
                case 3:
                    ARouter.getInstance().build(RConfig.MINE_VERIFY_RESULT)
                            .withBoolean("isSuccess", true)
                            .navigation();
                    break;
                case 4:
                    ARouter.getInstance().build(RConfig.MINE_VERIFY_RESULT)
                            .withBoolean("isSuccess", false)
                            .navigation();
                    break;
            }
        });
        //绑定手机号
        ClickUtil.click(menuBindMobile, () -> {
            if (!AppInnerCheckUtil.checkAuth(this, mUserInfo)) {
                return;
            }
            if (TextUtils.isEmpty(mUserInfo.getMobile())) {
                BindMobileActivity.start(this);
            } else {
                ToastUtil.showShort(getString(R.string.mine_securityActivity7));
            }
        });
        //登录密码
        ClickUtil.click(menuLoginPassword, () -> {
            if (!AppInnerCheckUtil.checkTel(this, mUserInfo)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_SET_PAY_PWD)
                    .withString("mobile", mUserInfo.getMobile())
                    .withInt("type", 0).navigation();
        });
        //交易密码
        ClickUtil.click(menuPayPassword, () -> {
            if (!AppInnerCheckUtil.checkTel(this, mUserInfo)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_SET_PAY_PWD)
                    .withString("mobile", mUserInfo.getMobile())
                    .withInt("type", 1 != mUserInfo.getIs_pwd() ? 1 : 2).navigation();
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SecurityActivity.class);
        context.startActivity(intent);
    }


    //
    public void updateStatus(UserData user) {
//        ImageUtil.setCircleImage(user.getUpic(), securityImg);
        mUserInfo = user;
        if (user == null) {
            return;
        }
        if (!TextUtils.isEmpty(user.getMobile())) {  // 手机号
            menuBindMobile.setRightString(getString(R.string.mine_securityActivity8));
        } else {
            menuBindMobile.setRightString(getString(R.string.mine_securityActivity9));
        }
        if (user.getIs_pwd() == 1) {  // 交易密码
            menuPayPassword.setRightString(getString(R.string.mine_securityActivity10));
        } else {
            menuPayPassword.setRightString(getString(R.string.mine_securityActivity11));
        }

        switch (user.getStatus()) {  // 实名认证状态
            case 1:
                menuVerify.setRightString(getString(R.string.mine_securityActivity12));
                break;
            case 2:
                menuVerify.setRightString(getString(R.string.mine_assetRecordsActivity3));
                break;
            case 3:
                menuVerify.setRightString(getString(R.string.mine_securityActivity14));
                switch (user.getAuth_status()) {  // 高级实名认证
                    case 1:
                        menuVerifyHigh.setRightString(getString(R.string.mine_securityActivity12));
                        break;
                    case 2:
                        menuVerifyHigh.setRightString(getString(R.string.mine_assetRecordsActivity3));
                        break;
                    case 3:
                        menuVerifyHigh.setRightString(getString(R.string.mine_securityActivity14));
                        break;
                    case 4:
                        menuVerifyHigh.setRightString(getString(R.string.mine_assetRecordsActivity5));
                        break;
                }
                break;
            case 4:
                menuVerify.setRightString(getString(R.string.mine_assetRecordsActivity5));
                break;
        }
        userViewModel.update();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.requestUserInfo();
    }
}
